package ThreadRunableVolite;

/**
 * 
 * @author zybw-kf01 2018-5-14 16:32:31
 * 
 * http://www.importnew.com/26850.html
 *如何让两个线程依次执行？
 *假设有两个线程，一个是线程 A，另一个是线程 B，
 *两个线程分别依次打印 1-3 三个数字即可。我们来看下代码：
 */
public class ThreadOrder1 {

	public static void main(String[] args) {

		Thread aThread = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("aThread this:" +this);
				printNumber("A");
			}
		});
		

		Thread bThread = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("bThread this:" +this);
				printNumber("B");
			}
		});


		aThread.start();
		bThread.start();

	}

	private static void printNumber(String threadName) {
		int i = 0;
		
		while (i++ < 3) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(threadName + " print: " + i);
		}
	}
}
